import dayjs from "dayjs";
import isoWeek from "dayjs/plugin/isoWeek";

// 使用 isoWeek 插件
dayjs.extend(isoWeek);

// 获取指定日期属于一年中的第几周
function getWeekNumber(date) {
  const d = new Date(date);
  d.setHours(0, 0, 0);
  d.setDate(d.getDate() + 4 - (d.getDay() || 7));
  const yearStart = new Date(d.getFullYear(), 0, 1);
  const weekNo = Math.ceil(((d - yearStart) / 86400000 + 1) / 7);
  return weekNo;
}

// 获取指定日期的年份
function getYear(date) {
  return new Date(date).getFullYear();
}

// 判断是否为闰年
function isLeapYear(year) {
  if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
    return true;
  } else {
    return false;
  }
}

// 获取一年中的周数
function getWeeksInYear(year) {
  // 获取年份的第一天，即1月1日
  const firstDayOfYear = new Date(year, 0, 1);
  // 获取第二天，用于判断是否为周末
  const secondDayOfYear = new Date(year, 0, 2);
  // 获取第一天是周几（0-6，0代表周日）
  const firstDayOfWeek = firstDayOfYear.getDay();
  // 获取第二天是周几
  const secondDayOfWeek = secondDayOfYear.getDay();
  // 如果第一天是周日，则年份开始于第一周，否则开始于第二周
  const startWeek = firstDayOfWeek === 0 ? 1 : 2;
  // 如果第二天是周六，则年份结束于第最后一周，否则结束于第二周
  let endWeek = secondDayOfWeek === 6 ? 53 : 52;
  // 如果第一天是周一，可能会有第53周
  if (firstDayOfWeek === 1) {
    endWeek = 53;
  }
  // 如果第二天是周日，并且第一天不是周一，则可能有第53和第54周
  if (secondDayOfWeek === 0 && firstDayOfWeek !== 1) {
    endWeek = 54;
  }
  return endWeek - startWeek + 1;
}

/**
 * @description 传入日期，返回这一周的开始和结束时间
 *  @param {Object} weekstr 格式：2023 32  这个32是周数
 */
function week_date(y: any, w: any) {
  const year = y;
  const weekNo = w;
  // 此年1号是星期几
  // console.log('调用时传的',year,weekNo);
  let oneday = new Date(year + "-01-01").getDay(); //0-6
  // console.log(oneday);
  // 方便计算，当为星期天时为7
  if (oneday == 0) {
    oneday = 7;
  }

  let one_fistday;
  let one_lastday;
  // 如果1号刚好是星期一
  if (oneday == 1) {
    one_fistday = year + "-01-01";
    one_lastday = year + "-01-07";
  } else {
    const jj = 8 - oneday;
    one_fistday =
      year -
      1 +
      "-12-" +
      (31 - oneday + 2 > 9 ? 31 - oneday + 2 : "0" + (31 - oneday + 2));
    one_lastday = year + "-01-" + (jj > 9 ? jj : "0" + jj);
  }

  let fistday;
  let lastday;
  // 如果刚好是第一周
  if (weekNo == 1) {
    fistday = one_fistday;
    lastday = one_lastday;
  } else {
    fistday = addDate(one_lastday, (weekNo - 2) * 7 + 1);
    lastday = addDate(one_lastday, (weekNo - 1) * 7);
  }
  return [fistday, lastday];
}

function addDate(date: any, days: any) {
  const d = new Date(date);
  d.setDate(d.getDate() + days);
  const m = d.getMonth() + 1;
  return (
    d.getFullYear() +
    "-" +
    (m > 9 ? m : "0" + m) +
    "-" +
    (d.getDate() > 9 ? d.getDate() : "0" + d.getDate())
  );
}

// 获取当前周有哪些天
function getDaysOfCurrentWeek(a: any) {
  const now = new Date(a);
  const dayOfWeek = now.getDay(); // 0 表示周日，6 表示周六
  const days = [];

  for (let i = 1; i <= 7; i++) {
    const date = new Date(now);
    date.setDate(date.getDate() - dayOfWeek + i); // 调整到本周的某一天
    days.push(dayjs(date).format("MM/DD")); // 将调整后的日期添加到数组中
  }

  return days;
}

// isoWeek
const globalShortcuts = [
  {
    text: "今日",
    value: () => {
      const end = dayjs().endOf("day");
      const start = dayjs().startOf("day");
      return [start, end];
    },
  },
  {
    text: "昨日",
    value: () => {
      const end = dayjs().subtract(1, "day").endOf("day");
      const start = dayjs().subtract(1, "day").startOf("day");
      return [start, end];
    },
  },
  {
    text: "本周",
    value: () => {
      const end = dayjs().endOf("isoWeek");
      const start = dayjs().startOf("isoWeek");
      return [start, end];
    },
  },
  {
    text: "上周",
    value: () => {
      const end = dayjs().subtract(1, "week").endOf("isoWeek");
      const start = dayjs().subtract(1, "week").startOf("isoWeek");
      return [start, end];
    },
  },
  {
    text: "本月",
    value: () => {
      const end = dayjs().endOf("month");
      const start = dayjs().startOf("month");
      return [start, end];
    },
  },
  {
    text: "上月",
    value: () => {
      const end = dayjs().subtract(1, "month").endOf("month");
      const start = dayjs().subtract(1, "month").startOf("month");
      return [start, end];
    },
  },
  {
    text: "今年",
    value: () => {
      const end = dayjs().endOf("year");
      const start = dayjs().startOf("year");
      return [start, end];
    },
  },
  {
    text: "去年",
    value: () => {
      const end = dayjs().subtract(1, "year").endOf("year");
      const start = dayjs().subtract(1, "year").startOf("year");
      return [start, end];
    },
  },
  {
    text: "近一周",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setDate(start.getDate() - 7);
      return [start, end];
    },
  },
  {
    text: "近一个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setMonth(start.getMonth() - 1);
      return [start, end];
    },
  },
  {
    text: "近三个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setMonth(start.getMonth() - 3);
      return [start, end];
    },
  },
];

export {
  getWeekNumber,
  getYear,
  getWeeksInYear,
  week_date,
  getDaysOfCurrentWeek,
  globalShortcuts,
};
